package com.block.merge.sdk.bridge;

import android.app.Activity;
import android.os.Handler;
import android.os.Looper;

import com.block.merge.sdk.BlockBusiness;

import java.lang.ref.WeakReference;
import java.lang.reflect.Method;
import java.util.LinkedList;

public class BlockCocosBridge implements BlockCocosIAP {

    private static BlockCocosBridge callbackApi;
    public static WeakReference<Activity> mainGameActivity;
    private Method evalString;
    private Method runOnGLThread;
    private static Handler handler = new Handler(Looper.getMainLooper());
    private volatile boolean GAME_ACTIVITY_PAUSED = false;
    private LinkedList<BlockCocosCallback> callbackList = new LinkedList<>();

    public static BlockCocosBridge getInstance() {
        if (callbackApi == null) {
            callbackApi = new BlockCocosBridge();
        }
        return callbackApi;
    }

    public void setMainGameActivity(Activity activity) {
        mainGameActivity = new WeakReference<>(activity);
        callbackList.clear();
    }

    @Override
    public void onMainGameActivityResume() {
        GAME_ACTIVITY_PAUSED = false;
        if (callbackList != null && !callbackList.isEmpty()){
            while (callbackList.size() > 0){
                BlockCocosCallback callback = callbackList.removeFirst();
                XSSdkCallback(callback.action, callback.jsonData);
            }
        }
    }

    @Override
    public void onMainGameActivityPause() {
        GAME_ACTIVITY_PAUSED = true;
    }

    @Override
    public void XSSdkCallback(String action, String jsonData) {

        try {
            if (evalString == null){
                Class clzBridge = Class.forName("org.cocos2dx.lib.Cocos2dxJavascriptJavaBridge", false, BlockBusiness.context.getClassLoader());
                evalString = clzBridge.getDeclaredMethod("evalString", String.class);
                evalString.setAccessible(true);
            }

            if (runOnGLThread == null){
                Class clzCocos2dAct = Class.forName("org.cocos2dx.lib.Cocos2dxActivity", false, BlockBusiness.context.getClassLoader());
                runOnGLThread = clzCocos2dAct.getDeclaredMethod("runOnGLThread", Runnable.class);
                runOnGLThread.setAccessible(true);
            }
        }catch (Exception e){
            e.printStackTrace();
        }


        if (mainGameActivity == null || mainGameActivity.get() == null) {
            return;
        }

        handler.post(new Runnable() {
            @Override
            public void run() {
                if (GAME_ACTIVITY_PAUSED) {
                    saveMessage(action,jsonData);

                    return;
                }
                try {
                    runOnGLThread.invoke(mainGameActivity.get(), new Runnable() {
                        @Override
                        public void run() {
                            try {
                                evalString.invoke(null, String.format("window.XSSdkCallback('%s','%s')", action, jsonData));
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

    }

    private void saveMessage(String action, String jsonData) {
        BlockCocosCallback blockCocosCallback = new BlockCocosCallback();
        blockCocosCallback.action = action;
        blockCocosCallback.jsonData = jsonData;
        callbackList.add(blockCocosCallback);
    }
}
